package service

import (
	"aglaia/internal/model"
	"aglaia/pkg/app"
)

func (svc *Service) Articles(page, pageSize int) (articles []model.Article, total int, err error) {
	return svc.dao.Articles(pageSize, app.GetPageOffset(page, pageSize))
}

//type QueryArticleRequest struct {
//	Id uint `json:"id"`
//}

func (svc *Service) QueryArticleById(id uint) (article model.ArticleContent, err error) {
	return svc.dao.QueryArticleById(id)
}

func (svc *Service) SearchArticle(data string) (articles []model.Article, err error) {
	return svc.dao.SearchArticle(data)
}

type CreateArticleRequest struct {
	Title         string `json:"title"`                    // 标题
	Desc          string `json:"desc"`                     // 文章简述
	CoverImageUrl string `json:"cover_image_url"`          // 封面图片
	Content       string `json:"content" gorm:"type:text"` // 正文
	CategoryId    uint   `json:"category_id"`              // 分类
	TagIds        []uint `json:"tag_ids"`                  // 标签id
}

func (svc *Service) CreateArticle(authId uint, article CreateArticleRequest) error {
	return svc.dao.CreateArticle(article.Title, article.Desc, article.CoverImageUrl, article.Content, authId, article.CategoryId, article.TagIds)
}

type UpdateArticleRequest struct {
	Id            uint   `json:"id"`
	Title         string `json:"title"`                    // 标题
	Desc          string `json:"desc"`                     // 文章简述
	CoverImageUrl string `json:"cover_image_url"`          // 封面图片
	Content       string `json:"content" gorm:"type:text"` // 正文
	State         uint8  `json:"state"`                    // 状态 0：禁用，1启用
	CategoryId    uint   `json:"category_id"`              // 分类
}

func (svc *Service) UpdateArticle(update UpdateArticleRequest) error {
	return svc.dao.UpdateArticle(update.Id, update.CategoryId, update.Title, update.Desc, update.CoverImageUrl, update.Content, update.State)
}

type DeleteArticleRequest struct {
	ArticleId uint `json:"article_id"`
}

func (svc *Service) DeleteArticle(del DeleteArticleRequest) error {
	return svc.dao.DeleteArticle(del.ArticleId)
}
